package com.future.servlet;

import com.future.biz.LoginBIZ;
import com.future.model.bo.UserBO;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

public class LoginServlet extends HttpServlet {
	private static Logger logger = LogManager.getLogger(LoginServlet.class);

	public LoginServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request,
				HttpServletResponse response) throws ServletException, IOException {

		logger.debug("doGet方法");
		doPost(request, response);
	}

	public void doPost(	HttpServletRequest request,
				HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("text/html;charset=utf-8");
		String flag = request.getParameter("flag");
		String code = request.getParameter("code");
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		UserBO userBo = null;
		LoginBIZ loginBiz = new LoginBIZ();
		try {
			if(flag!=null&&!("").equals(flag)&&"currentarea".equals(flag)){
				userBo = (UserBO) request.getSession().getAttribute("userBo");
				userBo = loginBiz.login(code+userBo.getUser().getUsername().substring(12).toString());
			}else {
				userBo = loginBiz.login(username, password);
				request.getSession().setAttribute("username", userBo.getUser().getUsername());
			}		
			loginForward(request, response, userBo);
		} catch (Exception e) {
			writeString(request, response, e.toString());
			logger.error(e);
		}
	}

	/**
	 * @param request
	 * @param response
	 * @param userBo
	 * @throws ServletException
	 * @throws java.io.IOException
	 */
	protected void loginForward(HttpServletRequest request,
															HttpServletResponse response,
															UserBO userBo) throws ServletException, IOException {
		request.getSession().setAttribute("userBo", userBo);
		// 根据用户级别及所属部门进行页面转发
		switch (userBo.getUserLevel()) {
		case 1:
		case 2:
		case 3:
			switch (userBo.getSurveyType()) {
			case 1:
				// 地方调查
				logger.debug("进入地方调查");
				request.getRequestDispatcher("/servlet/LocalSurveyServlet?flag=1").forward(request, response);
				updateSession(request);
				break;
			case 2:
			case 3:
				// 部门调查
				logger.debug("进入部门调查");
				request.getRequestDispatcher("/servlet/DepartmentServlet?flag=1").forward(request, response);
				updateSession(request);
				break;
			case 4:
				logger.debug("进入社区调查");
				request.getRequestDispatcher("/communityShowServices?flag=1").forward(request, response);
				updateSession(request);
				break;
			}
			break;
		case 4: // 街乡调查
			switch (userBo.getSurveyType()) {
			case 4:
				logger.debug("进入社区调查");
				request.getRequestDispatcher("/communityShowServices?flag=1").forward(request, response);
				updateSession(request);
				break;
			default:
				logger.debug("进入街乡调查");
				request.getRequestDispatcher("/showServices?flag=1").forward(request, response);
				updateSession(request);
				break;
			}
			break;
		}
	}

	public void init() throws ServletException {
	}

	public void writeString(HttpServletRequest request,
								HttpServletResponse response,
								String str) {
		response.setContentType("text/plain;charset=utf-8");
		PrintWriter out = null;
		try {
			request.setCharacterEncoding("utf-8");
			out = response.getWriter();
			out.print(str);
		} catch (UnsupportedEncodingException e) {
			logger.error(e);
		} catch (IOException e) {
			logger.error(e);
		} finally {
			out.flush();
			out.close();
		}
	}
	
	private void updateSession(HttpServletRequest request){
		 Object obj = request.getSession().getAttribute("username");
		 if(obj!=null){
			 String username = (String) obj;
			 if(username!=null&&!"".equals(username)){
				 UserBO userBo = (UserBO) request.getSession().getAttribute("userBo");
				 userBo.getUser().setUsername(username);
			 }
		 }
	}

}
